package cn.jdy.plugins.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.entity.ConfValue;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.Productx;

/**
 * @author daihu
 * @title : 产品操作实现
 */
@Service
public class ProductxService extends BaseDao<Productx> {

	@Autowired
	private ConfService confService;

	/**
	 * 按条件查询
	 * 
	 * @return
	 */
	public List<Productx> query(Productx productx, String... date) {
		if (productx == null)
			return super.findAll();
		if (productx.getiDisplayLength() > 0) {
			return super.find(createFilter(productx),
					productx.getiDisplayStart(), productx.getiDisplayLength());
		}
		return super.find(createFilter(productx));
	}

	public Long count(Productx productx, String... date) {
		return super.count(createFilter(productx));
	}

	/**
	 * 拼装条件
	 * 
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFilter(Productx productx, String... date) {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if(!StringUtil.isNullOrEmpty(productx.getKeywords())) { //后台管理用到这个地方
      if(!StringUtil.isNullOrEmpty(productx.getKeyType())) {
        dc.add(Restrictions.like(productx.getKeyType(), productx.getKeywords().trim(),MatchMode.ANYWHERE));
      }else
        //TODO 添加模糊搜索
        ;
    }
		if (!StringUtil.isNullOrEmpty(productx.getKeyword())) {
			dc.add(Restrictions.or(
					Restrictions.like("title", productx.getKeyword(), MatchMode.ANYWHERE),
					Restrictions.like("keyword", productx.getKeyword(), MatchMode.ANYWHERE),
					Restrictions.like("summary", productx.getKeyword(), MatchMode.ANYWHERE),
					Restrictions.like("context", productx.getKeyword(), MatchMode.ANYWHERE)));
		}
		if (date.length > 1 && !StringUtil.isNullOrEmpty(date[0])
				&& !StringUtil.isNullOrEmpty(date[1])) {
			dc.add(Restrictions.between("createTime", date[0], date[1]));
		}
		if (!StringUtil.isNullOrEmpty(productx.getStatus())) {
			dc.add(Restrictions.eq("status", productx.getStatus()));
		}
		if (!StringUtil.isNullOrEmpty(productx.getCounty())) {
      dc.add(Restrictions.eq("county", productx.getCounty()));
    }
		if (productx.getColumnId() != null && productx.getColumnId() > 0) {
			List<ConfValue> list = confService.findConfValueByPid(productx
					.getColumnId());
			if (list != null && list.size() > 0) {
				int size = list.size();
				Long[] ids = new Long[size];
				for (int i = 0; i < size; i++) {
					ids[i] = list.get(i).getId();
				}
				dc.add(Restrictions.in("columnId", ids));
			} else {
				dc.add(Restrictions.eq("columnId", productx.getColumnId()));
			}
		}
		if (!StringUtil.isNullOrEmpty(productx.getUserid())) {
			dc.add(Restrictions.eq("userid", productx.getUserid()));
		}
		if (!StringUtil.isNullOrEmpty(productx.getSortType())) {
			dc.addOrder(Order.desc("sortType"));
		}
		dc.addOrder(Order.desc("createTime"));
		return dc;
	}

	/**
	 * 
	 * @param productx
	 * @return
	 */
	public List<Productx> querypage(Productx productx) {
		return super.find(createFilter(productx),
				productx.getPageNo(), 4);
	}

	/**
	 * 查询热门产品
	 * 
	 * @return
	 */
	public List<Productx> queryHot() {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		dc.addOrder(Order.desc("readTimes"));
		return super.find(dc, 0, 5);

	}

	/**
	 * 按照类型查询 产品
	 * 
	 * @return
	 */
	public List<Productx> findbytype(Long columnid, int firstResult,
			int maxResults, boolean isImg) {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if (null != columnid) {
			dc.add(Restrictions.eq("columnId", columnid));
		}
		if (isImg) {// 是否有封面图片
			dc.add(Restrictions.ne("imgUrl", ""));
		}
		dc.addOrder(Order.desc("createTime"));
		return super.find(dc, firstResult, maxResults);
	}

	/**
	 * 按照大分类 查询产品
	 * 
	 * @return
	 */
	public List<Productx> findbyPid(Long pid, int firstResult, int maxResults,
			boolean isImg) {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		dc.createAlias("cv", "cv");
		if (isImg) {// 是否有封面图片
			dc.add(Restrictions.ne("imgUrl", ""));
		}
		if (null != pid) {
			dc.add(Restrictions.eq("cv.pid", pid));
		}
		dc.addOrder(Order.desc("createTime"));
		return super.find(dc, firstResult, maxResults);
	}

}
